<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>
			Ages of Strife: Admin panel
		</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
	include( "../global/ads.php" );
?>
			<div id="container">
				<div id="main-content">
					<div class="blok">
<?php

	include_once( "../global/old_database_connection.php" );
	$query = "SELECT * " .
			"FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$turns_to_inactive = $row[ 'turns_inactive' ];
	$current_turn = $row[ 'turn' ];
	$max_turn = $row[ 'max_turn' ];
	$update_time = $row[ 'update_time' ];
	$prizes_awarded = $row[ 'prizes_awarded' ];
	
	$turns_purchased = $row[ 'turns_purchased' ];
	$open_shop = $row[ 'open_shop' ];
	
	$query = "SELECT count(*) FROM ruler WHERE active_turn < " . ( $current_turn - $turns_to_inactive );
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$inactives = $row[ 'count(*)' ];
	
	$query = "SELECT count(*) FROM ruler WHERE start_turn != active_turn AND active_turn > 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$activeAfterStart = $row[ 'count(*)' ];
	
	$query = "SELECT count(*) " .
			"FROM ruler " .
			"JOIN ruler_task ON ruler_task.ruler_id = ruler.ruler_id " .
			"WHERE completed = 0 AND updated = 1 AND task_id = 1";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$notCheckedFirstTask = $row[ 'count(*)' ];
	
	$query = "SELECT count(*) " .
			"FROM ruler " .
			"WHERE ( SELECT count(*) " .
				"FROM medals " .
				"WHERE title LIKE '%campaign medal' AND ruler.user_id = medals.user_id ) = 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$noCampaignMedal = $row[ 'count(*)' ];
	
	$query = "SELECT count(*) " .
			"FROM ruler " .
			"JOIN users ON users.user_id = ruler.user_id " .
			"WHERE premium > 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$usersWithPremiumTurns = $row[ 'count(*)' ];

	$query = "SELECT SUM( civilian_news + military_news + operations_news + letters + news_letter + inactivity_notification ) " .
			"FROM ruler " .
			"JOIN users ON users.user_id = ruler.user_id";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$userSubscriptions = $row[ 'SUM( civilian_news + military_news + operations_news + letters + news_letter + inactivity_notification )' ];

	$query = "SELECT count(*) FROM ruler WHERE active_turn >= " . ( $current_turn - $turns_to_inactive );
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$actives = $row[ 'count(*)' ];
	
	$total = $actives + $inactives;

	$query = "SELECT SUM(turns) " .
			"FROM transactions " .
			"WHERE timestamp >= " . ( time() - 86400 );
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$lastDayPurchases = $row[ 'SUM(turns)' ];

	$query = "SELECT SUM(turns) " .
			"FROM transactions " .
			"WHERE timestamp >= " . ( time() - 604800 );
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$lastWeekPurchases = $row[ 'SUM(turns)' ];

	$query = "SELECT SUM(turns) " .
			"FROM transactions " .
			"WHERE timestamp >= " . ( time() - 2592000 );
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$lastMonthPurchases = $row[ 'SUM(turns)' ];
?>
			<strong>Rulers</strong><br />
			Active: <?php print( number_format( $actives ) ); ?> (<?php print( round( $actives / $total * 100, 2 ) ); ?>%)<br />
			Inactive: <?php print( number_format( $inactives ) ); ?> (<?php print( round( $inactives / $total * 100, 2 ) ); ?>%)<br />
			Users logged in after sign up turn: <?php print( number_format( $activeAfterStart ) ); ?> (<?php print( round( $activeAfterStart / $total * 100, 2 ) ); ?>%)<br />
			Users who did not check the first task: <?php print( number_format( $notCheckedFirstTask ) ); ?> (<?php print( round( $notCheckedFirstTask / $total * 100, 2 ) ); ?>%)<br />
			Users who don't have a campaign medal yet: <?php print( number_format( $noCampaignMedal ) ); ?> (<?php print( round( $noCampaignMedal / $total * 100, 2 ) ); ?>%)<br />
			Users with premium turns and playing: <?php print( number_format( $usersWithPremiumTurns ) ); ?> (<?php print( round( $usersWithPremiumTurns / $total * 100, 2 ) ); ?>%)<br />
			Email subscriptions for playing users: <?php print( number_format( $userSubscriptions ) . '/' . number_format( 6 * $total ) ); ?> (Average: <?php print( round( $userSubscriptions / $total, 2 ) ); ?> per player)<br />
			Total: <?php print( number_format( $total ) ); ?><br />
			<a href="ruler_stats.php">View more player information</a><br />
			<br />
<?php	
	if( $update_time != -1 ) {
		$update_time = round( $update_time * 0.001, 1 );
		if( $update_time > 60 ) {
			$minutes = floor( $update_time / 60 );
			$seconds = round( $update_time % 60 );
			$duration = $minutes . ' ' . translate( 'minutes' ) . ' ' . translate( 'and' ) . ' ' . $seconds . ' ' . translate( 'seconds' );
		}
		else {
			$update_time = $update_time . ' ' . translate( 'seconds' );
		}
		print( "Last update took: " . $update_time . '<br /><br />' );
	}
	else {
		print( "No recorded time for last update.<br /><br />" );
	}
	
	if( $open_shop == 1 ) {
?>
			Premium turns sold last 24 hours: <?php print( number_format( $lastDayPurchases ) ); ?><br />
			Premium turns sold last 7 days: <?php print( number_format( $lastWeekPurchases ) ); ?><br />
			Premium turns sold last 30 days: <?php print( number_format( $lastMonthPurchases ) ); ?><br />
			Premium turns sold all time: <?php print( number_format( $turns_purchased ) ); ?><br />
			<br />
<?php
		if( $current_turn == $max_turn ) {
			if( $prizes_awarded == 1 ) {
				print( "Round winners have already been awarded<br /><br />" );
			}
			else {
				?>
				Award prizes to top 3 economy points and top 3 war points:<br />
				<form method="post" action="award_prizes.php">
					<input type="submit" value="Award" />
				</form>
				<br />
				<br />
				<?php
			}
		} 
	}
	
    if( isset( $_SESSION[ 'store_stats_status' ] ) ) {
    	print( $_SESSION[ 'store_stats_status' ] . "<br />\r\n" );
    	unset( $_SESSION[ 'store_stats_status' ] );
    }
    
    if( $current_turn == $max_turn ) { 
		print( "<form method=\"post\" action=\"store_round_stats.php\">\r\n" );
		print( "<input type=\"submit\" name=\"submit\" value=\"Save current round settings\" /><br />\r\n" );
		print( "</form><br />\r\n" );
    }
	
    if( isset( $_SESSION[ 'lookup_ruler_status' ] ) ) {
    	print( $_SESSION[ 'lookup_ruler_status' ] . "<br />\r\n" );
    	unset( $_SESSION[ 'lookup_ruler_status' ] );
    }
    print( "Currently logged in as: " );
    if( !isset( $_SESSION[ 'ruler_id' ] ) ) {
    	$logged_in_as = "No one";
    }
    else {
    	$query = "SELECT name FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$logged_in_as = $row[ 'name' ];
    }
    print( "$logged_in_as<br />\r\n" );
    
    if( isset( $_SESSION[ 'ruler_id' ] ) ) {
    	$query = "SELECT ruler_id, name FROM ruler WHERE user_id = " . $_SESSION[ 'user_id' ];
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if( $row[ 'ruler_id' ] != $_SESSION[ 'ruler_id' ] ) {
			print( "<form method=\"post\" action=\"pick_ruler.php\">\r\n" );
			print( "<input type=\"hidden\" name=\"ruler_name\" value=\"" . $row[ 'name' ] . "\" />\r\n" );
			print( "<input type=\"submit\" name=\"submit\" value=\"Return to own ruler\" /><br />\r\n" );
			print( "</form><br />\r\n" );
		}
    }
    print( "<br />\r\n" );
	print( "<form method=\"post\" action=\"pick_ruler.php\">\r\n" );
	print( "Ruler lookup: <input type=\"text\" name=\"ruler_name\" />\r\n" );
	print( "<input type=\"submit\" name=\"submit\" value=\"submit\" /><br />\r\n" );
	print( "</form><br />\r\n" );
?>
						<strong>Links</strong><br />
						<a href="game_settings.php">Game settings</a><br />
						<a href="rulers.php">Active rulers sorted on IP address</a><br />
						<a href="possible_multies.php">Check for possible multies</a><br />
						<a href="languages.php">Manage languages</a><br />
						<a href="debug.php">Debug tools</a><br />
						<a href="chat_channels.php">Manage public chat channels</a><br />
						Trades: <a href="trades.php?type=public">public</a>, <a href="trades.php?type=faction">faction</a>, <a href="trades.php">private</a><br />
						<a href="send_newsletter.php">Write a newsletter</a><br />
<?php
	if( isset( $_SESSION[ 'ruler_id' ] ) ) {
		print( "\t\t\t\t\t<a href=\"../overview/overview.php\">Back to game overview</a><br/>\r\n" );
	}
?>
						------------------------------------<br />
						<a href="../index.php">Logout</a><br/>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>
